<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class WechatCard extends BaseModel
{
    // 表名
    protected $name = 'wechat_card';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    protected $resultSetType = 'collection';
    use  SoftDelete;

    // 追加属性
    protected $append = [
        'date_type_text',
        'use_custom_code_text',
        'get_custom_code_mode_text',
        'bind_openid_text',
        'can_share_text',
        'can_give_friend_text'
    ];

    //通过审核
    public static function passCheck($checkData)
    {
        $cardId = $checkData->CardId;
        if($checkData->Event == "card_pass_check"){
            Db('WechatCard')->where('card_id',$cardId)
                ->setField('pass_check',1);
            Db('WechatMemberCard')->where('card_id',$cardId)
                ->setField('pass_check',1);
        }
        Db('WechatCardCheck')->insert($checkData);
    }

    //用户领券 库存减1
    public static function subQuantity($cardId)
    {
        Db('WechatCard')->where('card_id',$cardId)
            ->setDec('quantity');
        Db('WechatMemberCard')->where('card_id',$cardId)
            ->setDec('quantity');
    }

    public function getBaseInfo()
    {
        $dataInfo['type'] = $this['date_type'];
        if($this['date_type'] == 'DATE_TYPE_FIX_TERM'){
            $dataInfo['fixed_term'] = $this['fixed_term'];
            $dataInfo['fixed_begin_term'] = $this['fixed_begin_term'];
        }elseif($this['date_type'] == 'DATE_TYPE_FIX_TIME_RANGE'){
            $dataInfo['begin_timestamp'] = $this['begin_timestamp'];
            $dataInfo['end_timestamp'] = $this['end_timestamp'];
        }
        $this->baseInfo = [
            'logo_url' => $this['wechat_logo_url'],
            'brand_name' => $this['brand_name'],
            'code_type' => isset($this['code_type']) ? $this['code_type'] : '',
            'title' => $this['title'],
            'sub_title' => $this['sub_title'],
            'color' => $this['color'],
            'notice' => $this['notice'],
            'service_phone' => $this['service_phone'],
            'description' => $this['description'],
            'date_info' => $dataInfo,
            'sku' => [
                'quantity' => $this['quantity']
            ],
            'location_id_list' => empty($this['location_id_list']) ? [] : explode(',', $this['location_id_list']),
            'get_limit' => $this['get_limit'],
            'use_custom_code'=>false,
            'get_custom_code_mode'=>false,
//            'use_custom_code'=>$this['use_custom_code'],
//            'get_custom_code_mode'=>$this['get_custom_code_mode'],
            'bind_openid' => boolval($this['bind_openid']),
            'can_share' => boolval($this['can_share']),
            'can_give_friend' => boolval($this['can_give_friend']),

            'center_title' => $this['center_title'],
            'center_sub_title' => $this['center_sub_title'],
            'center_url' => $this['center_url'],
            'custom_url_name' => $this['custom_url_name'],
            'custom_url' => $this['custom_url'],
            'custom_url_sub_title' => $this['custom_url_sub_title'],
            'promotion_url_name' => $this['promotion_url_name'],
            'promotion_url' => $this['promotion_url'],
            'source' => $this['source'],
        ];
    }

    public function getEspecial()
    {
        $especial =[];
        switch($this['card_type']){
            //通用优惠券
            case 'GENERAL_COUPON':
                $especial = [
                    'default_detail' => $this['default_detail']
                ];
                break;
            //代金券
            case 'CASH':
                $especial = [
                    'least_cost' => $this['least_cost']*100,
                    'reduce_cost' => $this['reduce_cost']*100
                ];
                break;
            //折扣券
            case 'DISCOUNT':
                $especial = [
                    'discount' => 100-$this['discount']*10
                ];
                break;
            //礼品券
            case 'GIFT':
                $especial = [
                    'gift' => $this['gift']
                ];
                break;
            //团购券
            case 'GROUPON':
                $especial = [
                    'deal_detail' => $this['deal_detail']
                ];
                break;
        }
        return $especial;
    }

    public function synchCard($data)
    {
        $cardType = strtolower($data['card_type']);
        $cardInfo = $data[$cardType];
        $baseInfo = $cardInfo['base_info'];
//        $advancedInfo = $cardInfo['advanced_info'];
        $card = self::getByCardId($baseInfo['id']);
        !$card && $card = new self();
        $card->mp_id = get_admin_applet_id();
        $card->card_id = $baseInfo['id'];
        $card->logo_url = $baseInfo['logo_url'];
        $card->card_type = $data['card_type'];
        $card->brand_name = $baseInfo['brand_name'];
        $card->title = $baseInfo['title'];
        $card->sub_title = $baseInfo['sub_title'];
        $card->color = $this->getColorCode($baseInfo['color']);
        $card->notice = $baseInfo['notice'];
        $card->description = $baseInfo['description'];
        $card->date_type = $baseInfo['date_info']['type'];
        if($card->date_type == 'DATE_TYPE_FIX_TERM'){
            $card->fixed_term = $baseInfo['date_info']['fixed_term'];
            $card->fixed_begin_term = $baseInfo['date_info']['fixed_begin_term'];
        }elseif($card->date_type == 'DATE_TYPE_FIX_TIME_RANGE'){
            $card->begin_timestamp = $baseInfo['date_info']['begin_timestamp'];
            $card->end_timestamp = $baseInfo['date_info']['end_timestamp'];
        }
        $card->quantity = $baseInfo['sku']['quantity'];
        $card->location_id_list = implode(',',$baseInfo['location_id_list']);
        $card->get_limit = $baseInfo['get_limit'];
        $card->can_share = $baseInfo['can_share'];
        $card->can_give_friend = $baseInfo['can_give_friend'];
//        $card->center_title = $baseInfo['center_title'];
//        $card->center_sub_title = $baseInfo['center_sub_title'];
//        $card->center_url = $baseInfo['center_url'];
        $card->custom_url_name = $baseInfo['custom_url_name'];
        $card->custom_url_sub_title = $baseInfo['custom_url_sub_title'];
        $card->custom_url = $baseInfo['custom_url'];
        $card->custom_url = $baseInfo['custom_url'];
//        $card->promotion_url_name = $baseInfo['promotion_url_name'];
//        $card->promotion_url = $baseInfo['promotion_url'];
//        $card->source = $baseInfo['source'];
        $card->status = $baseInfo['status'];
        $card->createtime = $baseInfo['create_time'];
        $card->updatetime = $baseInfo['update_time'];

        isset($baseInfo['service_phone']) && $card->service_phone = $baseInfo['service_phone'];
        //不同卡券类型 数据
        isset($cardInfo['discount']) && $card->discount = (100 - $cardInfo['discount'])/10;
        isset($cardInfo['least_cost']) && $card->least_cost = $cardInfo['least_cost']/100;
        isset($cardInfo['reduce_cost']) && $card->reduce_cost = $cardInfo['reduce_cost']/100;
        isset($cardInfo['gift']) && $card->gift = $cardInfo['gift'];
        isset($cardInfo['deal_detail']) && $card->deal_detail = $cardInfo['deal_detail'];
        isset($cardInfo['default_detail']) && $card->default_detail = $cardInfo['default_detail'];

        return $card->save();
    }

    public function getColorList()
    {
        return [
            'Color010'=>'#63b359',
            'Color020'=>'#2c9f67',
            'Color030'=>'#509fc9',
            'Color040'=>'#5885cf',
            'Color050'=>'#9062c0',
            'Color060'=>'#d09a45',
            'Color070'=>'#e4b138',
            'Color080'=>'#ee903c',
            'Color081'=>'#f08500',
            'Color082'=>'#a9d92d',
            'Color090'=>'#dd6549',
            'Color100'=>'#cc463d',
            'Color101'=>'#cf3e36',
            'Color102'=>'#5E6671',
        ];
    }

    public function getColorCode($colorValue)
    {
        foreach ($this->getColorList() as $code => $v) {
            if($colorValue == $v)
                return $code;
        }
        return 'Color010';
    }

    public function getDateTypeList()
    {
        return ['DATE_TYPE_FIX_TERM' => __('Type date_type_fix_term'),'DATE_TYPE_FIX_TIME_RANGE' => __('Type date_type_fix_time_range')];
    }

    public function getUseCustomCodeList()
    {
        return ['true' => __('Use_custom_code true'),'false' => __('Use_custom_code false')];
    }     

    public function getGetCustomCodeModeList()
    {
        return ['GET_CUSTOM_CODE_MODE_DEPOSIT' => __('Get_custom_code_mode_deposit'),'' => __('')];
    }     

    public function getBindOpenidList()
    {
        return ['true' => __('Bind_openid true'),'false' => __('Bind_openid false')];
    }     

    public function getCanShareList()
    {
        return ['true' => __('Can_share true'),'false' => __('Can_share false')];
    }     

    public function getCanGiveFriendList()
    {
        return ['true' => __('Can_give_friend true'),'false' => __('Can_give_friend false')];
    }     

    public function getDateTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['date_type'];
        $list = $this->getDateTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getUseCustomCodeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['use_custom_code'];
        $list = $this->getUseCustomCodeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getGetCustomCodeModeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['get_custom_code_mode'];
        $list = $this->getGetCustomCodeModeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getBindOpenidTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['bind_openid'];
        $list = $this->getBindOpenidList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCanShareTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['can_share'];
        $list = $this->getCanShareList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCanGiveFriendTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['can_give_friend'];
        $list = $this->getCanGiveFriendList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function setBeginTimestampAttr($value)
    {
        return strtotime($value);
    }
    public function setEndTimestampAttr($value)
    {
        return strtotime($value);
    }

}
